#include "imageenhancethread.h"

#include <QLibrary>
#include <QDir>

#define IE_SYMBOL "LaunchImageEnhancer_RAWnMem"

ImageEnhanceThread::ImageEnhanceThread(QObject *parent) :
    m_src(0),
    m_dest(0),
    m_w(0),
    m_h(0),
    m_bits(0),
    QThread(parent)
{
}

void ImageEnhanceThread::setIeLibPath(const QString &libPath)
{
    LaunchImageEnhancer_RAWnMem = (T_LaunchImageEnhancer_RAWnMem)QLibrary::resolve(libPath, IE_SYMBOL);
    libDir = libPath;
    libDir.replace("/", "\\");
    libDir = libDir.left(libDir.lastIndexOf("\\")+1);
}

void ImageEnhanceThread::run()
{
    int ret = 0;
    QString err;

    if (LaunchImageEnhancer_RAWnMem) {
        if (m_iee.isEmpty()) {
            err = tr("IE config file not specified, image enhance ignored.");
        } else {
            m_iee = libDir + m_iee;
            m_iee.replace("\\", QDir::separator());
            m_iee.replace("/", QDir::separator());
            ret = LaunchImageEnhancer_RAWnMem(m_src, m_dest, m_iee.toLocal8Bit().data(), m_w, m_h, m_bits);
            if (!ret) err = tr("Lib internal error.");
        }
    } else {
        err = tr("Image enhance ibrary not found or symbol(s) unresolved.");
    }

    emit resultReady(ret, err);
}
